<template>
    <div>
        <!-- 面包屑导航 -->
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>


        <!-- 卡片视图 -->
        <el-card>
            <el-row :gutter="20">
                <el-col :span="8">
                    <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable>
                        <el-button slot="append" @click="getGoodsList" icon="el-icon-search"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="4">
                    <el-button type="primary" @click="getAddpage()">添加商品</el-button>
                </el-col>
            </el-row>

            <!-- table表格 -->
            <el-table :data="getGoodList" border stripe>

                <!-- 序号列 -->
                <el-table-column label="#" type="index">
                </el-table-column>

                <!-- 商品名称 -->
                <el-table-column label="商品名称" prop="goods_name" width="570px">
                </el-table-column>

                <!-- 商品价格(元) -->
                <el-table-column width="125px" label="商品价格(元)" prop="goods_price">
                </el-table-column>

                <!-- 商品重量 -->
                <el-table-column width="100px" label="商品重量" prop="goods_weight">
                </el-table-column>

                <!-- 创建时间 -->
                <el-table-column label = "创建时间" width="170px">
                    <template v-slot="scope">
                        {{scope.row.add_time | dateFormat}}
                    </template>
                </el-table-column>


                <!-- 操作 -->
                <el-table-column label="操作" width="148px">
                    <template v-slot="scope">

                    <!-- 修改按钮 -->
                    <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>

                    <!-- 删除按钮 -->
                    <el-button type="danger" icon="el-icon-delete" size="mini" @click="shanChu(scope.row.goods_id)"></el-button>
                </template>
                </el-table-column>
            </el-table>

            <!-- 分页功能 -->
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum"
                :page-sizes="[1, 3, 5, 10]"
                :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="this.total">
            </el-pagination>
        </el-card>

    </div>
</template>
<script>
export default {
    data() {
        return {

            input3:'',
            queryInfo:{

                //查询参数 
                query:'',

                //当前页码
                pagenum:1,

                //分页条数
                pagesize:10
            },
            total:0,
            getGoodList:[]
        }
    },
    created() {
        this.getGoodsList()
    },
    methods: {

        //获取商品数据列表
        async getGoodsList(){
            const { data } = await this.axios.get('goods',{params:this.queryInfo})

            if(data.meta.status !== 200){
                return this.$message.error('获取用户失败')
            }

            
            //console.log(data) 
            this.getGoodList = data.data.goods
            this.total = data.data.total
        },
        handleSizeChange(news){
            this.queryInfo.pagesize= news;
            this.getGoodsList()
        },
        handleCurrentChange(old){
            this.queryInfo.pagenum  = old;
             this.getGoodsList()
        },

        //删除
        async shanChu(id){


            const  data1  = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).catch(err=>err)
            if(data1 == 'confirm'){
                const { data } = await this.axios.delete('goods/'+id)
                //console.log(data)
                this.getGoodsList()
                this.$message.success('删除用户成功')
            }
        },

        //添加商品
        getAddpage(){
            this.$router.push('/goods/add')
        }

    },
}
</script>
<style lang="less" scoped>
    .el-table{
        margin: 14px 0;
    }
</style>